import request from '@/router/axios';
import Mock from 'mockjs';
import { Random } from 'mockjs'
//手机号
// 创建自定义 Mock 函数
Random.extend({
    // 自定义函数名: function 函数  
    telFn: function() {    
      const phonePrefix = ['132', '135', '189','156','138','181','186']
      const index = Math.floor(Math.random() * phonePrefix.length)
      var phone = phonePrefix[index] + Mock.mock(/\d{8}/);
      return this.pick(phone)
  }
})
//id号
// var ids = Mock.mock({"id|1-300":1000})


let list = Mock.mock({
    "tableList|10-30":[
        {   
            username:'@cname',
            'id|1-300':500, // 记录id
            'userId|1-30':300, // 用户编号
            "type|0-1":10,  // 类型
            "amount|1-200.2": 1, // 体现金额
            "totalAmount|1-300.2":1, // 已提金额
            phone:'@telFn()',
            traceSn:'@integer(10000)', // 申请流水
            "apptype|0-2":10, // 审批状态 0通过  1拒接 2待审批
            appro:0,//操作审批 0审批  1已审批
            status:'',// 审批备注
        }
    ]
})

//数据列表
export const MoneyList = (data) =>request({
    url:'/MoneyList',
    method:'get',
    data
})

Mock.mock(/\/MoneyList/,config=>{
    let {pagenum,pagesize} = JSON.parse(config.body);
    let _list = list.tableList;
    let total = _list.length;
    if(pagenum && pagesize){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                total,
                user,pagenum,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
            
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
   
})

//删除数据
export const detMoney = (data) =>request({
    url:'/detMoney',
    mothed:'delete',
    data
})
Mock.mock(/\/detMoney/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
    
})

//查询 根据用户名  手机号  体现状态 
// username phone apptype
export const queryList = (data) =>request({
    url:'/queryList',
    mothed:'get',
    data
})
Mock.mock(/\/queryList/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let arr = [];
    if(datas.username || datas.phone || datas.apptype.length>0){
        if(datas.username){
            arr = _list.filter((item,index)=>item.username.indexOf(datas.username)!=-1);
        }
        if(datas.phone){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.phone.indexOf(datas.phone)!=-1);
        }
        if(datas.apptype.length>0){
            let str = JSON.stringify(datas.apptype)
            arr = (arr.length==0?_list:arr).filter((item,index)=>str.indexOf(item.apptype)>=0);
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'查询成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 审批
export const editMoney = (data) =>request({
    url:'/editMoney',
    mothed:'put',
    data
})
Mock.mock(/\/editMoney/,config=>{
    let datas = JSON.parse(config.body);
    let {id,apptype} = datas;
    let _list = list.tableList;
    let num = 0;
    if(id && (apptype==0 || apptype==1)){
        for(let item of _list){
            if(item.id==id){
                if(item.appro == 0){
                    item.status = datas.status?datas.status:item.status;
                    item.appro = 1;
                    item.apptype = apptype;
                }else{
                    num = 1
                }
            }
        }
        if(num == 1){
            return {
                meta:{
                    msg:'错误',
                    status:400
                }
            }
        }else{
            return {
                meta:{
                    msg:'审批成功',
                    status:200
                }
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
    
})